package com.ljkj.blockpay.ui.activity;

import androidx.annotation.Nullable;
import androidx.core.content.FileProvider;
import androidx.fragment.app.Fragment;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileUtils;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.ljkj.blockpay.BuildConfig;
import com.ljkj.blockpay.R;
import com.ljkj.blockpay.basedata.BaseActivity;
import com.ljkj.blockpay.databinding.ActivityMyprofileBinding;
import com.ljkj.blockpay.presenter.MyProfilePresenter;
import com.ljkj.blockpay.util.GlideEngine;
import com.ljkj.blockpay.widget.ClickListener;
import com.ljkj.blockpay.widget.CommonDialog;
import com.tbruyelle.rxpermissions3.Permission;
import com.tbruyelle.rxpermissions3.RxPermissions;
import java.io.File;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

/**
 * 我的资料页面
 */
public class MyProfileActivity extends BaseActivity<ActivityMyprofileBinding, MyProfilePresenter> implements ClickListener {


    private CommonDialog mPhotoDialog;
    private static final int ALBUM_CODE = 100;
    String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
    public static final int PICTURE_CROPPING_CODE = 200;
    public static final String[] permissionsGroup =new String[]{

            Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};


    @Override
    public int initContentView() {
        return R.layout.activity_myprofile;
    }

    @Override
    protected MyProfilePresenter createPresenter() {
        return null;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        binding.setOnClick(this::onClick);
    }

    @Override
    protected void initData() {
        super.initData();

        View inflate = LayoutInflater.from(baseContext).inflate(R.layout.uploadway_layout, null, false);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.iv_userphoto:
                mPhotoDialog = CommonDialog.Builder.with(this)
                        .setContentView(R.layout.uploadway_layout)
                        .forGravity(Gravity.BOTTOM)
                        .setCancelable(true)
                        .setOnClickListener(R.id.tv_uploadalbum, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                new RxPermissions(MyProfileActivity.this).requestEachCombined(permissionsGroup).subscribe(new Observer<Permission>() {
                                    @Override
                                    public void onSubscribe(@NonNull Disposable d) {

                                    }

                                    @Override
                                    public void onNext(@NonNull Permission permission) {
                                        Intent intent_album = new Intent("android.intent.action.GET_CONTENT");
                                        intent_album.setType("image/*");
                                        startActivityForResult(intent_album, ALBUM_CODE);
                                    }

                                    @Override
                                    public void onError(@NonNull Throwable e) {

                                    }

                                    @Override
                                    public void onComplete() {

                                    }
                                });

                            }
                        })
                        .setOnClickListener(R.id.tv_uploadphoto, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                new RxPermissions(MyProfileActivity.this).requestEachCombined(Manifest.permission.CAMERA)
                                        .subscribe(new Observer<Permission>() {
                                            @Override
                                            public void onSubscribe(@NonNull Disposable d) {

                                            }

                                            @Override
                                            public void onNext(@NonNull Permission permission) {
//                                                PictureSelector.create(MyProfileActivity.this)
//                                                        .openCamera(SelectMimeType.ofImage())
//                                                        .forResult(new OnResultCallbackListener<LocalMedia>() {
//                                                            @Override
//                                                            public void onResult(ArrayList<LocalMedia> result) {
//
//
//                                                            }
//
//                                                            @Override
//                                                            public void onCancel() {
//
//                                                            }
//                                                        });
                                            }

                                            @Override
                                            public void onError(@NonNull Throwable e) {

                                            }

                                            @Override
                                            public void onComplete() {

                                            }
                                        });
                            }
                        })
                        .setOnClickListener(R.id.btn_cancel, new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mPhotoDialog.cancel();
                            }
                        })
                        .fullWidth()
                        .setOnKeyListener((dialog, keyCode, event) -> false)
                        .create();
                mPhotoDialog.show();
                break;

            case R.id.rl_username:


                break;

            case R.id.rl_phonenumber:
                break;

            case R.id.rl_email:
                break;

            default:

                break;



        }

    }

    /**
     * 图片剪裁
     *
     * @param uri 图片uri
     */
    private void clip(Uri uri) {
        //隐式拉起系统的裁剪CropActivity
        Intent intent = new Intent("com.android.camera.action.CROP");
        //通过FileProvider进行通讯
        if (Build.VERSION.SDK_INT >= 24) {
            uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID + ".fileprovider", new File(uri.getPath()));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.setDataAndType(uri, "image/*");
        } else {
            intent.setDataAndType(uri, "image/*");
        }
        // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪
        intent.putExtra("crop", "true");
        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        // outputX outputY 是裁剪图片宽高
        intent.putExtra("outputX", 150);
        intent.putExtra("outputY", 150);
        intent.putExtra("circleCrop", true);
        if (Build.VERSION.SDK_INT >= 30) {
            //android 11以上，将文件创建在公有目录
            String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath();
                    File mOnputFile = new File(path, System.currentTimeMillis() + ".png");
            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file://" + mOnputFile.getAbsolutePath()));
        } else {
            //storage/emulated/0/Android/data/com.xxxxx/cache
//            File mOnputFile = new File(sdPath, System.currentTimeMillis() + ".png");
//            intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.parse("file://" + mOnputFile.getAbsolutePath()));
        }
//        startActivityForResult(intent, ICON_CROP);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);



        if (data!= null || data.getData() == null){
            return;
        }
//        clip(Uri.fromFile(new File(ImageFileUtils.getPath(this, data.getData()))));
    }
}